﻿Imports Oracle.DataAccess.Client
Public Class fListaCCostos
    Private lCcostos As String
    Public ReadOnly Property CCosto() As String
        Get
            Return lCcostos
        End Get
    End Property

    Private Sub cargaTipoCC()
        Dim daDatTablas As lbGlobalcom.dsGlobalTableAdapters.GLO_DATOSTABLASTableAdapter
        daDatTablas = New lbGlobalcom.dsGlobalTableAdapters.GLO_DATOSTABLASTableAdapter
        Try
            daDatTablas.FillByIdgrupo(DsGlobal.GLO_DATOSTABLAS, "cc")
            Dim dr As lbGlobalcom.dsGlobal.GLO_DATOSTABLASRow = DsGlobal.GLO_DATOSTABLAS.NewGLO_DATOSTABLASRow
            dr.ID = -1
            dr.IDGRUPO = "cc"
            dr.DESCRIPCION = "Todas"
            DsGlobal.GLO_DATOSTABLAS.Rows.Add(dr)
            dr = Nothing
            Me.cbTipoCCostos.SelectedValue = -1
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub
    Private Sub fListaCCostos_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            cargaTipoCC()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try

    End Sub

    Private Sub btnConsulta_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConsulta.Click
        Dim qWhere As String = "WHERE "
        Dim qSelect As String = "SELECT * FROM CON_CCOSTO "
        Dim CuentaW As Int16 = 0
        If txtCCostos.Text.Trim.Length > 0 Then
            CuentaW += 1
            qWhere &= "DESCRIPCION LIKE '%" & Me.txtCCostos.Text & "%'"
        End If
        If cbTipoCCostos.SelectedValue <> -1 Then
            If CuentaW > 0 Then _
                qWhere &= " AND "
            CuentaW += 1
            qWhere &= "TIPO = " & Me.cbTipoCCostos.SelectedValue
        End If
        If CuentaW = 0 Then _
            qWhere = ""
        Dim Conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
        Dim cmd As OracleCommand = Conn.CreateCommand
        cmd.CommandText = qSelect + qWhere
        Try
            Conn.Open()
            DsContabilidad.CON_CCOSTO.Rows.Clear()
            DsContabilidad.CON_CCOSTO.Load(cmd.ExecuteReader)
            Conn.Close()
            CON_CCOSTODataGridView.Focus()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub CON_CCOSTODataGridView_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles CON_CCOSTODataGridView.CellMouseDoubleClick
        If e.RowIndex >= 0 Then
            lCcostos = Me.CON_CCOSTODataGridView.Rows(e.RowIndex).Cells("IDCCOSTO").Value
            Me.DialogResult = Windows.Forms.DialogResult.OK
        End If
    End Sub

    Private Sub AsignaCcosto(ByVal Fila As Integer)
        If Fila >= 0 Then
            lCcostos = CON_CCOSTODataGridView.Rows(Fila).Cells("IDCCOSTO").Value
            Me.DialogResult = Windows.Forms.DialogResult.OK
        End If
    End Sub

    Private Sub CON_CCOSTODataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles CON_CCOSTODataGridView.KeyDown
        If e.KeyData = Keys.Enter Then
            AsignaCcosto(CON_CCOSTODataGridView.CurrentRow.Index)
        End If
    End Sub
End Class